/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */

/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
 var lowestCommonAncestor = function(root, p, q) {
    if(root === null) {
      return root
    }
    if(root.val > q.val && root.val > q.val){
      let left = lowestCommonAncestor(root.left,p,q)
      return left
    }
    if(root.val < q.val && root.val < q.val){
      let right = lowestCommonAncestor(root.right,p,q)
      return right
    }
    return root
};